package com.heima.schedule.controller;

import com.heima.model.common.dto.ResponseResult;
import com.heima.model.schedule.dto.TaskDto;
import com.heima.schedule.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/task")
@Slf4j
public class ScheduleController {

    @Autowired
    private TaskService taskService;

    /**
     * 添加任务
     *
     * @param task 任务对象
     * @return 任务id
     */
    @PostMapping("/add")
    public ResponseResult addTask(@RequestBody TaskDto task) {
        log.info("addTask，TaskDto：{}", task);
        return ResponseResult.okResult(taskService.addTask(task));
    }

    /**
     * 取消任务
     *
     * @param taskId 任务id
     * @return 取消结果
     */
    @GetMapping("/cancel/{taskId}")
    public ResponseResult cancelTask(@PathVariable("taskId") long taskId) {
        log.info("cancelTask，taskId：{}", taskId);
        return ResponseResult.okResult(taskService.cancelTask(taskId));
    }

    /**
     * 按照类型和优先级来拉取任务
     *
     * @param type
     * @param priority
     * @return
     */
    @GetMapping("/poll/{type}/{priority}")
    public ResponseResult poll(@PathVariable("type") int type, @PathVariable("priority") int priority) {
        log.info("pollTask，taskId：{},{}", type, priority);
        return ResponseResult.okResult(taskService.poll(type, priority));
    }
}
